//
//  InformationViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/20.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "InformationViewController.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "MainTabBarController.h"
#import "InformationCell.h"
#import "MeterialsCell.h"
#import "InformationModel.h"
#import "ImageModel.h"

@interface InformationViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>
{
    NSArray *_contenArray;
}

#define TitleArr @[@"用户信息",@"图文信息",@"风控保障"]
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *informationArrM;
@property (nonatomic, strong) NSMutableArray *imageArrM;
@end




@implementation InformationViewController

- (BorrowGetDetailModel *)model {
    if (!_model) {
        _model = [[BorrowGetDetailModel alloc] init];
    }
    return _model;
}

- (NSMutableArray *)informationArrM {
    if (!_informationArrM) {
        _informationArrM = [NSMutableArray array];
    }
    return _informationArrM;
}

- (NSMutableArray *)imageArrM {
    if (!_imageArrM) {
        _imageArrM = [NSMutableArray array];
    }
    return _imageArrM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"项目信息";
    
      [self.view addSubview:self.myTableView];
    [self getBorrowDetailNew];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.num == 2) {
        [self getUserInfo];
        [self getBorrowImage];
    }
    
}

- (void)getBorrowImage {
    NSDictionary *dict = @{@"BorrowId":self.model.BorrowId};
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Borrow.GetBorrowImage" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
       
        [self.imageArrM removeAllObjects];
        
        NSArray *arr = [object[@"Data"] firstObject][@"ItemList"];
        for (NSDictionary *dic in arr) {
           ImageModel *model = [ImageModel mj_objectWithKeyValues:dic];
            [self.imageArrM addObject:model];
        }
        
        [self.myTableView reloadData];
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@", error);
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:^(float progress) {
        
    }];
}

- (void)getUserInfo {
    NSDictionary *dict = @{
                           @"LoginName":self.model.BLoginId
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"User.GetUserInfo" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
        NSDictionary *dic = [object[@"Data"] firstObject];
        if (dic) {
            InformationModel *model = [InformationModel mj_objectWithKeyValues:dic];
            
            
            [self.informationArrM removeAllObjects];
            [self.informationArrM addObject:model];
            [self.myTableView reloadData];
        }
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:^(float progress) {
        
    }];
}


-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64-40) style:UITableViewStyleGrouped];
        }
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.backgroundColor = [UIColor whiteColor];
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableView.showsVerticalScrollIndicator = NO;
    return _myTableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 1;
    }else if (section == 1){
        return self.imageArrM.count;
    }else
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        InformationCell *informationCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!informationCell) {
            informationCell = [[[NSBundle mainBundle] loadNibNamed:@"InformationCell" owner:self options:nil] firstObject];
        }
        NSString *userName = [NSString stringWithFormat:@"%@", self.model.BLoginId];
        NSRange r = {3,6};
        userName = [userName stringByReplacingCharactersInRange:r withString:@"******"];
        informationCell.userName.text = [NSString stringWithFormat:@"用户名：%@", userName];
        [informationCell setDataWithModel:[self.informationArrM firstObject]];
        [informationCell cellAutoLayoutHeight:self.model.Introduction];
        informationCell.selectionStyle =  UITableViewCellSelectionStyleNone;
        return informationCell;
    } else if (indexPath.section == 1){
        MeterialsCell *meterialsCell = [tableView dequeueReusableCellWithIdentifier:@"MeterialsCell"];
        if (!meterialsCell) {
            meterialsCell = [[[NSBundle mainBundle] loadNibNamed:@"MeterialsCell" owner:self options:nil] firstObject];
        }
        if (self.imageArrM.count != 0) {
            
            ImageModel *model = self.imageArrM[indexPath.row];
            [meterialsCell.firstImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ImageUrl,model.Picsmall]] placeholderImage:[UIImage imageNamed:@"SHOUYE_02"]];
            
        }
        
        return meterialsCell;
    }else{
        UITableViewCell *bbcell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
        if (bbcell == nil) {
            bbcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
        }
        
        UIWebView *webView = [[UIWebView alloc] initWithFrame:bbcell.frame];
    
        [bbcell.contentView addSubview:webView];
        [webView loadHTMLString:self.model.Riskcontrol baseURL:nil];
        return bbcell;
    
   
    }
}


//返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        InformationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InformationCell"];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"InformationCell" owner:self options:nil] lastObject];
        }
        [cell cellAutoLayoutHeight:self.model.Introduction];
        CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
        return size.height;
    }else if (indexPath.section == 2){
        // 计算label的高度
        // 根据对应的文字求出cell上label显示的高度
        NSDictionary *fontDic =[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14 ],NSFontAttributeName, nil];
        
        // 根据字的大小 ,计算出文本的尺寸
        // 还需要执行一个尺寸(375, 0)
        // 第三个参数:计算高度需要的依据字体的哪个特征来确定
        CGRect rect=[self.model.Riskcontrol boundingRectWithSize:CGSizeMake(Screen_Width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
        
        // 最后把结果作为返回值返回
        
        return rect.size.height;
    }
    
    else {
        return 200;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40)];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 40)];
    lable.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    lable.text = [NSString stringWithFormat:@"    %@",TitleArr[section]];
    [header addSubview:lable];
    
    return header;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

//Borrow.GetBorrowDetailNew
-(void)getBorrowDetailNew{
    NSDictionary *para = @{
                           @"BorrowId":self.Borrowid,
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Borrow.GetBorrowDetailNew" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:nil];
}

@end
